package com.mes.web.controller.jy;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mes.entity.tdd.Staff;
import com.mes.entity.tdd.tj.Tpage;
import com.mes.service.jy.LoginService;
import com.mes.service.tdd.StaffService;
import com.mes.utils.MD5;
import com.mes.utils.ResponseUtil;

/**
 * @ClassName: LoginController 
 * @Description:登录
 * @Author jy
 * @Date 2018年3月14日 上午9:35:09 
 * @version 1.0
 */
@Controller
public class LoginController {
	
	@Resource
	private LoginService loginService;
	@Resource
	public StaffService staffService;
	
	/**
	 * 登录
	 * @param response 响应
	 * @throws IOException 异常
	 */
	@RequestMapping("/login")
	public void login(HttpServletResponse response) throws IOException {
		response.getWriter().print("layout");
	}
	
	/**
	 * 登录校验
	 * @param session 会花
	 * @param response 响应
	 * @param passwords 秘密
	 * @param staffnumbering 员工编号
	 * @throws IOException IO异常
	 */
	@RequestMapping("/loginCheck")
	public void loginCheck(HttpSession session,HttpServletResponse response,String passwords,String staffnumbering) throws IOException {
		//参数判断是否为空或是否为null
		if (null != staffnumbering && null != passwords && !staffnumbering.equals("") && !passwords.equals("") ) {
			Tpage tpage = new Tpage();
			tpage.setIsdelete("0");
			tpage.setPasswords(MD5.md5Encode(passwords));
			tpage.setStaffnumbering(staffnumbering);
			List<Staff> staffs = staffService.all(tpage);
			if (staffs.size() == 1) {
				session.setAttribute("staff", staffs.get(0));
				response.getWriter().print(1);
			}else{
				response.getWriter().print(0);
			}
		}else{
			response.getWriter().print(0);
		}
	}
	
	/**
	 * 登录
	 * @param session 会话
	 * @return 返回页面
	 * @throws IOException IO异常
	 */
	@RequestMapping("/toLogin")
	public String toLogin(HttpSession session) throws IOException {
		 //从session中取出用户身份信息
        Staff staffSession = (Staff) session.getAttribute("staff");
		if(staffSession != null){
			session.invalidate(); 
		}
		return "/reLogin";
	}
	
	/**
	 * 检查员工代码是否存在
	 * @param name 员工名称1
	 * @param name1 员工名称2
	 * @param response 响应
	 * @throws IOException IO异常
	 */
	@RequestMapping("/checkNameisExist")
	public void checkNameisExist(String name,String name1,HttpServletResponse response) throws IOException {
		int affct = 0;
		if(name != null && name1 != null && name != "" &&  name1 != "") {
			//两个名字都不为空
			int count = loginService.checkNameisExist(name);
			if(count >0) {
				int count2 = loginService.checkNameisExist(name1);
				if(count2 >0) {
					affct = 1;
				}
			}else {
				affct = -1;
			}
		}else if(name1 == null ||  name1 == ""){
			int count = loginService.checkNameisExist(name);
			if(count > 0) {
				affct = 1;
			}
		}else if(name == null || name == "") {
			int count2 = loginService.checkNameisExist(name1);
			if(count2 >0) {
				affct = 1;
			}
		}
		//查询员工是否空闲。（未实现）
		response.getWriter().print(affct);
	}
	
	/**
	 * 登录校验
	 * @param session 会话
	 * @param response 响应
	 * @param passwords 密码
	 * @param staffnumbering 员工编号
	 * @throws Exception 异常
	 */
	@RequestMapping("/loginCheckARP")
	public void loginCheckARP(HttpSession session,HttpServletResponse response,String passwords,String staffnumbering) throws Exception {
		response.setContentType("application/json; charset=utf-8"); 
		net.sf.json.JSONObject jsonObject = new net.sf.json.JSONObject();
		if (null != staffnumbering && null != passwords && !staffnumbering.equals("") && !passwords.equals("") ) {
			Tpage tpage = new Tpage();
			tpage.setIsdelete("0");
			tpage.setPasswords(passwords);
			tpage.setStaffnumbering(staffnumbering);
			List<Staff> staffs = staffService.all(tpage);
			if (staffs.size() == 1) {
				//传递数据到页面
				jsonObject.put("code", "200");
				jsonObject.put("message", "");
				jsonObject.put("results", staffs.get(0));
			}else{
				//传递数据到页面
				jsonObject.put("code", "500");
				jsonObject.put("message", "请输入正确的密码和用户工号");
				jsonObject.put("results", null);
			}
		}else{
			//传递数据到页面
			jsonObject.put("code", "500");
			jsonObject.put("message", "请输入正确的密码和用户工号");
			jsonObject.put("results", null);
		}
		ResponseUtil.write(response, jsonObject);
	}

}
